using System;
using System.Data;
using System.Collections.Generic;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ClickSocial.Components;
using ClickSocial.Entities;
using ClickSocial.Framework;

namespace ClickSocial.Admin
{
    public partial class SetUser : System.Web.UI.UserControl
    {
        private string m_evento_id="0";
        //private Evento m_evento_mapper = new Evento();

        public string EventoID
        {
            get
            {
                m_evento_id = ViewState["EventoID"] as string ?? string.Empty;
                return m_evento_id;
            }
            set
            {
                m_evento_id = value;
                ViewState["EventoID"] = m_evento_id;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        public void LoadUserEvent()
        {
            if (EventoID != "0")//es editable
            {
                string[] users = Evento.GetUsersInEvent(long.Parse(EventoID));
                if (users.Length > 0)
                {
                    ListBoxUsersEvent.DataSource = users;
                    ListBoxUsersEvent.DataBind();
                }
                BindData();
            }
        }

        public void BindData()
        {
            ListBoxUsers.Items.Clear();
            foreach (MembershipUser u in Membership.GetAllUsers())
            {
                if (Roles.IsUserInRole(u.UserName, Global.RolesApp.Host))
                    continue;
                if (ListBoxUsersEvent.Items.Count>0 && ListBoxUsersEvent.Items.FindByText(u.UserName) != null)
                    continue;
                if (Roles.IsUserInRole(u.UserName, Global.RolesApp.UserEvent))
                    ListBoxUsers.Items.Add(new ListItem(u.UserName, u.UserName));
            }
        }

        protected void lnkBtnAdd_Click(object sender, EventArgs e)
        {
            if (ListBoxUsers.SelectedItem != null)
            {
                Evento.AddUserEvent(long.Parse(EventoID), ListBoxUsers.SelectedValue);
                ListBoxUsersEvent.Items.Add(ListBoxUsers.SelectedItem);
                ListBoxUsers.Items.Remove(ListBoxUsers.SelectedItem);
            }
            BindData();
        }

        protected void lnkBtnDel_Click(object sender, EventArgs e)
        {
            if (ListBoxUsersEvent.SelectedItem != null)
            {
                Evento.DelUserEvent(long.Parse(EventoID), ListBoxUsersEvent.SelectedValue);
                ListBoxUsers.Items.Add(ListBoxUsersEvent.SelectedItem);
                ListBoxUsersEvent.Items.Remove(ListBoxUsersEvent.SelectedItem);
                
            }
            BindData();
        }
   
    }
}